package com.gwnet.shop.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.gwnet.common.log.annotation.Log;
import com.gwnet.common.log.enums.BusinessType;
import com.gwnet.common.mybatis.core.page.TableDataInfo;
import com.gwnet.common.satoken.utils.LoginHelper;
import com.gwnet.shop.domain.ShopAddress;
import com.gwnet.shop.service.IShopAddressService;
import com.gwnet.shop.utils.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 收货地址Controller
 * 
 * @author 张万鹏
 * @date 2024-07-22
 */
@RestController
@RequestMapping("/address")
public class ShopAddressController extends MBaseController
{
    @Resource
    private IShopAddressService shopAddressService;

    /**
     * 查询收货地址列表
     */
    @SaCheckPermission("shop:address:list")
    @GetMapping("/list")
    public TableDataInfo list(ShopAddress memberAddress)
    {
        startPage();
        List<ShopAddress> list = shopAddressService.selectShopAddressList(memberAddress);
        return getDataTable(list);
    }


    /**
     * 获取收货地址详细信息
     */
    @SaCheckPermission("shop:address:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(shopAddressService.selectShopAddressById(id));
    }

    /**
     * 新增收货地址
     */
    @SaCheckPermission("shop:address:add")
    @Log(title = "收货地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopAddress memberAddress)
    {
        memberAddress.setCreateBy( LoginHelper.getUserId() );
        return toAjax(shopAddressService.insertShopAddress(memberAddress));
    }

    /**
     * 修改收货地址
     */
    @SaCheckPermission("shop:address:edit")
    @Log(title = "收货地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopAddress memberAddress)
    {
        memberAddress.setCreateBy( LoginHelper.getUserId() );
        return toAjax(shopAddressService.updateShopAddress(memberAddress));
    }

    /**
     * 删除收货地址
     */
    @SaCheckPermission("shop:address:remove")
    @Log(title = "收货地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shopAddressService.deleteShopAddressByIds(ids));
    }
}
